package com.lanou.myapp.comic.themecomic.contentthemecomic;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.bumptech.glide.Glide;
import com.lanou.myapp.R;
import com.lanou.myapp.imageloader.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/18.
 */
public class ContentThemeComicAdapter extends BaseAdapter {
    private Context context;
    private List<ContentThemeComicBeans.ComicsEntity> data;
//    private ImageLoader imageLoader;
//    private ImageLoader.ImageCache imageCache;
//    private LruCache<String, Bitmap> lruCache;
//    private RequestQueue queue;


    public ContentThemeComicAdapter(Context context) {
        this.context = context;
//        init();
    }

    public void addData(List<ContentThemeComicBeans.ComicsEntity> data) {
        this.data = data;
        notifyDataSetChanged();
    }


//    private void init() {
//        queue = Volley.newRequestQueue(context);
//        lruCache = new LruCache<String, Bitmap>(20);
//        imageCache = new ImageLoader.ImageCache() {
//            @Override
//            public Bitmap getBitmap(String url) {
//                return lruCache.get(url);
//            }
//
//            @Override
//            public void putBitmap(String url, Bitmap bitmap) {
//                lruCache.put(url, bitmap);
//            }
//        };
//        //构建imageLoader对象
//        imageLoader = new ImageLoader(queue, imageCache);
//    }

//    public void getBitmapCache(String imgUrl, ImageView imageView) {
//        // 构建图片监听器
//        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.mipmap.img_guide_p1,
//                R.mipmap.img_guide_p1);
//        // 图片拉取
//        imageLoader.get(imgUrl, listener, 300, 300);
//    }



    @Override
    public int getCount() {
        return data.size() > 0 && data != null ? data.size()/2 : 0;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null) {
            myViewHolder = new MyViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_content_theme_comic, null);
            myViewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv_item_content_theme_comic);
            myViewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name_item_content_theme_comic);
            myViewHolder.tv_type = (TextView) convertView.findViewById(R.id.tv_type_item_content_theme_comic);
            myViewHolder.tv_description = (TextView) convertView.findViewById(R.id.tv_description_item_theme_comic);
            convertView.setTag(myViewHolder);
        }else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }

//        String img_url = data.get(position).getCover();
//        getBitmapCache(img_url, myViewHolder.imageView);
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(myViewHolder.imageView,R.mipmap.v6_boy_unseletor_icon,R.mipmap.v6_boy_unseletor_icon);
        VolleySingleton.getImageLoader().get(data.get(position).getCover(),listener,400,300);


//        Glide.with(context).load(data.get(position).getCover()).into(myViewHolder.imageView);


        myViewHolder.tv_name.setText(data.get(position).getName());
        myViewHolder.tv_type.setText(data.get(position).getRecommend_brief());
        myViewHolder.tv_description.setText(data.get(position).getRecommend_reason());
        return convertView;
    }

    class MyViewHolder {
        private ImageView imageView;
        private TextView tv_name, tv_type, tv_description;
    }

}
